﻿using Dapper;
using Dapper.Contrib.Extensions;
using Newtonsoft.Json;
using SmartForm.Common;
using SmartForm.Rule;
using SmartlForm.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartlForm.DAL
{
    /// <summary>
    /// 发送状态
    /// </summary>
    public enum SendStatus
    {
        /// <summary>
        /// 未发送
        /// </summary>
        UNSENT = 0,
        /// <summary>
        /// 进行中
        /// </summary>
        HAVE = 1,
        /// <summary>
        /// 已发送
        /// </summary>
        SEND = 2,

    }

    public class TaskArticlePlatformDao
    {
        public readonly static TaskArticlePlatformDao Instance = new TaskArticlePlatformDao();
        /// <summary>
        /// 添加发送记录
        /// </summary>
        /// <returns></returns>
        public long AddSendRecord(long account_id, SystemRule systemRule)
        {
            TaskArticlePlatform model = new TaskArticlePlatform();
            model.account_id = account_id;
            model.content = JsonConvert.SerializeObject(systemRule);
            model.platform = systemRule.Platform;
            model.send_status = SendStatus.SEND.ToString();
            model.send_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            return DataBase.DB.Insert(model);
        }

        /// <summary>
        /// 今日发送数据
        /// </summary>
        /// <param name="account_id"></param>
        /// <returns></returns>
        public long GetNowCount(long account_id)
        {
            string c_time = DateTime.Now.ToString("yyyy-MM-dd");
            string sql = string.Format("select count(1) from task_article_platform WHERE send_time LIKE '{0}%' AND account_id={1}", c_time, account_id);
            Console.WriteLine(sql);
            return DataBase.DB.QueryFirstOrDefault<long>(sql);
        }
    }
}
